"""
Description:返回参数的类
"""
from dataclasses import dataclass
from typing import TypeVar, Generic, Optional
from ..enums.app_http_code_enum import AppHttpCodeEnum

T = TypeVar('T')


@dataclass
class ResponseResult(Generic[T]):
    host: Optional[str] = None
    code: int = 200
    error_message: Optional[str] = None
    data: Optional[T] = None

    # 由类方法调用
    def error(self, code: int, error_message: Optional[str], data=None) -> 'ResponseResult':
        self.code = code
        self.error_message = error_message
        self.data = data
        return self

    def ok_one(self, code: int, data: Optional[T]) -> 'ResponseResult':
        self.code = code
        self.data = data
        return self

    def ok_two(self, code: int, data: Optional[T] = None, error_message: Optional[str] = None) -> 'ResponseResult':
        self.code = code
        self.data = data
        self.error_message = error_message
        return self

    def set_app_http_code_enum_default_msg(self, enums: AppHttpCodeEnum):
        return self.ok_result_with_code_msg(enums.code, enums.error_message)

    def set_app_http_code_enum_with_msg(self, enums: AppHttpCodeEnum, msg: str):
        return self.ok_result_with_code_msg(enums.code, msg)

    @classmethod
    def error_result(cls, code: int, error_message: Optional[str], data=None):
        response_result = ResponseResult()
        return response_result.error(code, error_message, data)

    @classmethod
    def error_result_with_enums(cls, enums: AppHttpCodeEnum):
        return cls().set_app_http_code_enum_with_msg(enums, enums.error_message)

    @classmethod
    def ok_result_with_code_msg(cls, code: int, error_message: Optional[str]):
        response_result = ResponseResult()
        return response_result.ok_two(code=code, error_message=error_message)

    @classmethod
    def ok_result_with_default_code(cls, msg, data: Optional[T] = None):
        instance = cls()
        response_result = instance.set_app_http_code_enum_with_msg(enums=AppHttpCodeEnum.SUCCESS, msg=msg)
        if data is not None:
            response_result.data = data
        return response_result
